package com.geor.gcv.core.process;

import org.opencv.core.Mat;
import org.opencv.core.Rect;

import com.geor.gcv.core.mat.Mats;
import com.geor.gcv.core.model.Area;

/**
 * 截图
 * 
 * @author geor.lusongnan
 *
 */
public class ScreenShootProcess extends Process {

	private Process process;
	private Area area;

	public ScreenShootProcess(Process process, Area area) {
		super();
		this.process = process;
		this.area = area;

	}

	@Override
	public Mat process() {
		Mat img = process.process();
		int x = area.getX();
		int y = area.getY();
		int row = area.getHeight();
		int col = area.getWidth();
		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		if (x >= img.cols()) {
			x = img.cols() - 2;
			col = 1;
		} else if ((x + col) >= img.cols()) {
			col = img.cols() - x - 1;
		}
		if (y >= img.rows()) {
			y = img.rows() - 2;
			row = 1;
		} else if ((row + y) >= img.rows()) {
			row = img.rows() - y - 1;
		}

		Rect rect = new Rect(x, y, col, row);
		Mat roi_img = new Mat(img, rect);
		Mat tmp_img = new Mat();

		roi_img.copyTo(tmp_img);
		
		Mats.releseMat(roi_img);
		// Mat result = new Mat(row, col, img.type());
		// for (int i = 0; i < result.rows(); i++) {
		// for (int j = 0; j < result.cols(); j++) {
		// result.put(i, j, img.get(y + i, x + j));
		// }
		// }
		return tmp_img;
	}

	@Override
	public String getProcessName() {
		return process.getProcessName() + "-->截图";
	}
}
